package com.example.androidone;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.Display;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.view.SurfaceHolder;
import android.util.Log;
import android.view.ViewGroup;

import com.example.androidone.player.VideoDecoder;
import com.example.androidone.usb.MockUsbDataReceiver;
import com.example.androidone.utils.Constants;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private SurfaceView surfaceView;
    private VideoDecoder decoder;
    private MockUsbDataReceiver mockReceiver;
    private Button btnStart;
    private Button btnStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        initDecoder();
    }

    private void initViews() {
        surfaceView = findViewById(R.id.surfaceView);
        btnStart = findViewById(R.id.btnStart);
        btnStop = findViewById(R.id.btnStop);

        // 获取屏幕尺寸
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;
        int screenHeight = size.y;

        // 计算适合屏幕的视频显示尺寸
        float videoRatio = (float) Constants.VIDEO_WIDTH / Constants.VIDEO_HEIGHT;
        float screenRatio = (float) screenWidth / screenHeight;

        int displayWidth;
        int displayHeight;

        if (screenRatio > videoRatio) {
            // 屏幕比视频更宽，以高度为基准
            displayHeight = screenHeight;
            displayWidth = (int) (displayHeight * videoRatio);
        } else {
            // 屏幕比视频更窄，以宽度为基准
            displayWidth = screenWidth;
            displayHeight = (int) (displayWidth / videoRatio);
        }

        // 设置 SurfaceView 尺寸
        ViewGroup.LayoutParams params = surfaceView.getLayoutParams();
        params.width = displayWidth;
        params.height = displayHeight;
        surfaceView.setLayoutParams(params);

        // 设置 Surface 的像素格式
        surfaceView.getHolder().setFormat(android.graphics.PixelFormat.RGBA_8888);

        // 设置固定大小
        surfaceView.getHolder().setFixedSize(Constants.VIDEO_WIDTH, Constants.VIDEO_HEIGHT);

        // 直接添加 Surface 回调
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Log.d(TAG, "Surface created");
                decoder.init(holder.getSurface(), Constants.VIDEO_WIDTH, Constants.VIDEO_HEIGHT);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                Log.d(TAG, "Surface changed: " + width + "x" + height);
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                Log.d(TAG, "Surface destroyed");
                decoder.release();
            }
        });

        // 设置按钮点击事件
        setupButtons();
    }

    private void setupButtons() {
        btnStart.setOnClickListener(v -> {
            if (mockReceiver != null) {
                btnStart.setEnabled(false);
                btnStop.setEnabled(true);
                mockReceiver.start();
                Log.d(TAG, "Started mock receiver");
            }
        });

        btnStop.setOnClickListener(v -> {
            if (mockReceiver != null) {
                btnStop.setEnabled(false);
                btnStart.setEnabled(true);
                mockReceiver.stop();
                Log.d(TAG, "Stopped mock receiver");
            }
        });

        // 初始状态设置
        btnStart.setEnabled(true);
        btnStop.setEnabled(false);
    }
    private void initDecoder() {
        try {
            decoder = new VideoDecoder();
            mockReceiver = new MockUsbDataReceiver(this, decoder);
            Log.d(TAG, "Initialized decoder and mock receiver");
        } catch (Exception e) {
            Log.e(TAG, "Error initializing decoder", e);
        }
    }

    private void releaseDecoder() {
        if (decoder != null) {
            decoder.release();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (surfaceView.getHolder().getSurface().isValid()) {
            decoder.init(surfaceView.getHolder().getSurface(),
                    Constants.VIDEO_WIDTH, Constants.VIDEO_HEIGHT);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mockReceiver != null) {
            mockReceiver.stop();
        }
        releaseDecoder();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mockReceiver != null) {
            mockReceiver.stop();
        }
        releaseDecoder();
        Log.d(TAG, "Activity destroyed");
    }
}